Skip to contentMethod: static {...}
1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.owl2java;
19:
20: import cz.cvut.kbss.jopa.owl2java.cli.CliParams;
21: import cz.cvut.kbss.jopa.owl2java.cli.Command;
22: import cz.cvut.kbss.jopa.owl2java.cli.Option;
23: import cz.cvut.kbss.jopa.owl2java.config.Defaults;
24: import cz.cvut.kbss.jopa.owl2java.config.TransformationConfiguration;
25: import joptsimple.OptionParser;
26: import joptsimple.OptionSet;
27: import org.slf4j.Logger;
28: import org.slf4j.LoggerFactory;
29:
30: import java.io.PrintStream;
31: import java.util.Arrays;
32: import java.util.Optional;
33:
34: public class OWL2Java {
35:
36: private static final Logger LOG = LoggerFactory.getLogger(OWL2Java.class);
37:
38: private static void printHelp(Command cc) {
39: final PrintStream os = System.out;
40:
41: os.print(cc.helpText);
42: try {
43: cc.parser.printHelpOn(os);
44: } catch (Exception e) {
45: LOG.error(e.getMessage(), e);
46: }
47: }
48:
49: private static Optional<Command> getCommand(String s) {
50: try {
51: return Optional.of(Command.valueOf(s));
52: } catch (IllegalArgumentException e) {
53: return Optional.empty();
54: }
55: }
56:
57: public static void main(String[] args) {
58:
59: if (args.length == 0) {
60: System.out.println("Syntax: OWL2Java <command> <args>. Run 'OWL2Java help' for more details");
61: return;
62: }
63:
64: final Optional<Command> c = getCommand(args[0]);
65:
66: if (c.isEmpty()) {
67: System.err
68: .println("Invalid command " + args[0] + ", try 'OWL2Java help' for the list of available commands");
69: return;
70: }
71:
72: final OptionParser op = c.get().parser;
73: final OptionSet os = op.parse(args);
74: final CliParams input = new CliParams(os);
75:
76: final OWL2JavaTransformer oj;
77:
78: switch (c.get()) {
79: case help:
80: if (args.length != 1) {
81: final Optional<Command> cc = getCommand(args[1]);
82: if (cc.isPresent()) {
83: printHelp(cc.get());
84: } else {
85: System.err.println("Invalid command " + args[0] + " " + args[1] +
86: ", try 'OWL2Java help' for the list of available commands.");
87: return;
88: }
89: } else {
90: System.out.println("Available commands : " + Arrays.asList(Command.values()));
91: }
92: break;
93: case list:
94: if (invalidArgumentCount(input)) {
95: break;
96: }
97: oj = getTransformer(input);
98:
99: System.out.println("Available contexts: " + oj.listContexts());
100: break;
101: case transform:
102: if (invalidArgumentCount(input)) {
103: break;
104: }
105: transformOwlToJava(input);
106: break;
107: case vocabulary:
108: if (invalidArgumentCount(input)) {
109: break;
110: }
111: generateVocabulary(input);
112: break;
113: case version:
114: System.out.println("OWL2Java version " + Constants.VERSION);
115: break;
116: default:
117: System.err.println("Unknown command '" + args[0] + "', try 'OWL2Java help'.");
118: }
119: }
120:
121: private static OWL2JavaTransformer getTransformer(CliParams input) {
122: OWL2JavaTransformer oj;
123: oj = new OWL2JavaTransformer();
124: if (input.has(Option.MAPPING_FILE.arg)) {
125: oj.setOntology(input.nonOptionArguments().get(1), input.valueOf(Option.MAPPING_FILE.arg).toString());
126: } else {
127: oj.setOntology(input.nonOptionArguments().get(1), null);
128: }
129: oj.ignoreMissingImports(input.is(Option.IGNORE_FAILED_IMPORTS.arg, Defaults.IGNORE_FAILED_IMPORTS));
130: return oj;
131: }
132:
133: private static boolean invalidArgumentCount(CliParams input) {
134: if (input.nonOptionArguments().size() != 2) {
135: System.err
136: .println("Exactly one ontology IRI has to be specified, got "
137: + (input.nonOptionArguments().size() - 1)
138: + ", try 'OWL2Java help' for the list of available commands");
139: return true;
140: }
141: return false;
142: }
143:
144: private static void transformOwlToJava(CliParams input) {
145: boolean whole = input.is(Option.WHOLE_ONTOLOGY_AS_IC.arg, Defaults.WHOLE_ONTOLOGY_AS_IC);
146:
147: if (!whole && invalidTransformationOptions(input)) {
148: return;
149: }
150:
151: final TransformationConfiguration config = TransformationConfiguration.config(input);
152:
153: final OWL2JavaTransformer transformer = getTransformer(input);
154:
155: transformer.transform(config);
156: }
157:
158: private static boolean invalidTransformationOptions(CliParams input) {
159: if (invalidArgumentCount(input)) {
160: return true;
161: }
162:
163: if (!input.has(Option.CONTEXT.arg)) {
164: System.err.println("The parameter '-" + Option.CONTEXT.arg +
165: "' is obligatory. Try the 'help' command for more details.");
166: return true;
167: }
168: return false;
169: }
170:
171: private static void generateVocabulary(CliParams input) {
172: final boolean whole = input.is(Option.WHOLE_ONTOLOGY_AS_IC.arg, Defaults.WHOLE_ONTOLOGY_AS_IC);
173: if (!whole && invalidTransformationOptions(input)) {
174: return;
175: }
176: final OWL2JavaTransformer transformer = getTransformer(input);
177:
178: final TransformationConfiguration config = TransformationConfiguration.config(input);
179:
180: transformer.generateVocabulary(config);
181: }
182: }